﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QBFC7Lib;

namespace QuickBooks.DataAccess
{
    public static class CustomerHelper
    {
        public static void Save(this Customer customer)
        {
            if (customer.Persisted)
            {
                throw new NotImplementedException();
            }
            else
            {
                QBSessionManager session = new QBSessionManagerClass();
                IMsgSetRequest request = session.CreateMsgSetRequest("US", 7, 0); //todo - figure out what this does

                BuildAddCustomerRequest(request);

                session.OpenConnection(string.Empty, "Order Manager");
                session.BeginSession(string.Empty, ENOpenMode.omDontCare);

                IMsgSetResponse response = session.DoRequests(request);

                session.EndSession();
                session.CloseConnection();
                session = null;
            }
        }

        public static void Delete(this Customer customer)
        {
            throw new NotImplementedException();
        }

        internal static void Load(this Customer customer, System.Xml.Linq.XDocument doc)
        {
            throw new NotImplementedException();
        }

        public static IEnumerable<Customer> GetAll()
        {
            QBSessionManager session = new QBSessionManagerClass();

            IMsgSetRequest request = session.CreateMsgSetRequest("US", 7, 0); //todo - figure out what this does

            ICustomerMsgQuery retRequest = request.AppendCustomerMsgQueryRq();
            
            session.OpenConnection("AMI_OM", "Order Manager");
            session.BeginSession(@"C:\Users\Public\Documents\Intuit\QuickBooks\Company Files\Afton Manufacturing, Inc.QBW", ENOpenMode.omDontCare);

            IMsgSetResponse response = session.DoRequests(request);

            session.EndSession();
            session.CloseConnection();
            session = null;

            yield return null; //hack
        }

        private static void BuildAddCustomerRequest(IMsgSetRequest request)
        {
            ICustomerAdd addRequest = request.AppendCustomerAddRq();

            addRequest.Name.SetValue("Bill");
            addRequest.IsActive.SetValue(true);
            addRequest.ParentRef.ListID.SetValue("52");
            addRequest.ParentRef.FullName.SetValue("Bill Ding");
            addRequest.CompanyName.SetValue("Billding Inc.");
            addRequest.Salutation.SetValue("Howdy!");
            addRequest.FirstName.SetValue("Bill");
            addRequest.MiddleName.SetValue("Ill");
            addRequest.LastName.SetValue("Ding");
            addRequest.BillAddress.Addr1.SetValue("ab");
            addRequest.BillAddress.Addr2.SetValue("ab");
            addRequest.BillAddress.Addr3.SetValue("ab");
            addRequest.BillAddress.Addr4.SetValue("ab");
            addRequest.BillAddress.Addr5.SetValue("ab");
            addRequest.BillAddress.City.SetValue("ab");
            addRequest.BillAddress.State.SetValue("ab");
            addRequest.BillAddress.PostalCode.SetValue("ab");
            addRequest.BillAddress.Country.SetValue("ab");
            addRequest.BillAddress.Note.SetValue("ab");
            addRequest.ShipAddress.Addr1.SetValue("ab");
            addRequest.ShipAddress.Addr2.SetValue("ab");
            addRequest.ShipAddress.Addr3.SetValue("ab");
            addRequest.ShipAddress.Addr4.SetValue("ab");
            addRequest.ShipAddress.Addr5.SetValue("ab");
            addRequest.ShipAddress.City.SetValue("ab");
            addRequest.ShipAddress.State.SetValue("ab");
            addRequest.ShipAddress.PostalCode.SetValue("ab");
            addRequest.ShipAddress.Country.SetValue("ab");
            addRequest.ShipAddress.Note.SetValue("ab");
            addRequest.Phone.SetValue("ab");
            addRequest.AltPhone.SetValue("ab");
            addRequest.Fax.SetValue("ab");
            addRequest.Email.SetValue("ab");
            addRequest.Contact.SetValue("ab");
            addRequest.AltContact.SetValue("ab");
            addRequest.CustomerTypeRef.ListID.SetValue("200000-1011023419");
            addRequest.CustomerTypeRef.FullName.SetValue("ab");
            addRequest.TermsRef.ListID.SetValue("200000-1011023419");
            addRequest.TermsRef.FullName.SetValue("ab");
            addRequest.SalesRepRef.ListID.SetValue("200000-1011023419");
            addRequest.SalesRepRef.FullName.SetValue("ab");
            addRequest.OpenBalance.SetValue(10.01);
            addRequest.OpenBalanceDate.SetValue(DateTime.Parse("12/15/2007"));
            addRequest.SalesTaxCodeRef.ListID.SetValue("200000-1011023419");
            addRequest.SalesTaxCodeRef.FullName.SetValue("ab");
            addRequest.ItemSalesTaxRef.ListID.SetValue("200000-1011023419");
            addRequest.ItemSalesTaxRef.FullName.SetValue("ab");
            addRequest.ResaleNumber.SetValue("ab");
            addRequest.AccountNumber.SetValue("ab");
            addRequest.CreditLimit.SetValue(10.01);
            addRequest.PreferredPaymentMethodRef.ListID.SetValue("200000-1011023419");
            addRequest.PreferredPaymentMethodRef.FullName.SetValue("ab");
            addRequest.CreditCardInfo.CreditCardNumber.SetValue("ab");
            addRequest.CreditCardInfo.ExpirationMonth.SetValue(6);
            addRequest.CreditCardInfo.ExpirationYear.SetValue(6);
            addRequest.CreditCardInfo.NameOnCard.SetValue("ab");
            addRequest.CreditCardInfo.CreditCardAddress.SetValue("ab");
            addRequest.CreditCardInfo.CreditCardPostalCode.SetValue("ab");
            addRequest.JobStatus.SetValue(ENJobStatus.jsAwarded);
            addRequest.JobStartDate.SetValue(DateTime.Parse("12/15/2007"));
            addRequest.JobProjectedEndDate.SetValue(DateTime.Parse("12/15/2007"));
            addRequest.JobEndDate.SetValue(DateTime.Parse("12/15/2007"));
            addRequest.JobDesc.SetValue("ab");
            addRequest.JobTypeRef.ListID.SetValue("52");
            addRequest.JobTypeRef.FullName.SetValue("ab");
            addRequest.Notes.SetValue("ab");
            addRequest.PriceLevelRef.ListID.SetValue("52");
            addRequest.PriceLevelRef.FullName.SetValue("ab");
            addRequest.IncludeRetElementList.Add("ab");
        }
    }
}